package com.sunseagear.wind.modules.test.treeandtable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.test.treeandtable.entity.TreeAndTable;
import com.sunseagear.wind.modules.test.treeandtable.service.ITreeAndTableService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @version V1.0
 * @package test.treeandtable
 * @title: 左树右表控制器
 * @description: 左树右表控制器
 * @author: admin
 * @date: 2019-11-13 21:24:49
 * @copyright: www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("test/treeandtable/treeandtable")
@RequiredArgsConstructor
@Log(title = "左树右表")
public class TreeAndTableController extends BaseBeanController {

    private final ITreeAndTableService treeAndTableService;

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @PostMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("test:treeandtable:treeandtable:list")
    public RPage<TreeAndTable> list(HttpServletRequest request) {
        //加入条件
        QueryWrapper<TreeAndTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_date");
        String name = request.getParameter("name");
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.eq("name", name);
        }
        String type = request.getParameter("type");
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        String areaId = request.getParameter("areaId");
        if (!StringUtils.isEmpty(areaId)) {
            queryWrapper.eq("area_id", areaId);
        }
        // 预处理
        Page<TreeAndTable> pageBean = treeAndTableService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("test:treeandtable:treeandtable:add")
    public R<Void> add(@Validated TreeAndTable entity) {
        treeAndTableService.save(entity);
        return R.success("添加成功");
    }

    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("test:treeandtable:treeandtable:update")
    public R<Void> update(@Validated TreeAndTable entity) {
        treeAndTableService.saveOrUpdate(entity);
        return R.success("更新成功");
    }

    @GetMapping("detail/{id}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("test:treeandtable:treeandtable:detail")
    public R<TreeAndTable> detail(@PathVariable("id") Long id) {
        TreeAndTable treeAndTable = treeAndTableService.getById(id);
        return R.success(treeAndTable);
    }

    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("test:treeandtable:treeandtable:delete")
    public R<Void> delete(@RequestParam("ids") List<Long> idList) {
        treeAndTableService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
